#include <GL/glut.h>
#include "canvas.h"
#include "Point3D.h"
#include <iostream>

#define screenWidth 800
#define screenHight 600
#define screenPosHor 240
#define screenPosVer 100

Point3D centro(2,2,2), posCam(1,1,1);
//<<<<<<<<<<<<<<<<<<< axis >>>>>>>>>>>>>>
void axis(double length)
{ // draw a z-axis, with cone at end
	glPushMatrix();
	glBegin(GL_LINES);
	   glVertex3d(0, 0, 0); glVertex3d(0,0,length); // along the z-axis
	glEnd();
	glTranslated(0, 0,length -0.2); 
	glutWireCone(0.04, 0.2, 12, 9);
	glPopMatrix();
}	
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<< displayWire >>>>>>>>>>>>>>>>>>>>>>
void displayWire(void)
{
	glMatrixMode(GL_PROJECTION); // set the view volume shape
	glLoadIdentity();
	glOrtho(-2.0*64/48.0, 2.0*64/48.0, -2.0, 2.0, 0.1, 100);
	glMatrixMode(GL_MODELVIEW); // position and aim the camera
	glLoadIdentity();
	gluLookAt(posCam.getX(),posCam.getY(),posCam.getZ(),centro.getX(),centro.getY(),centro.getZ(),0,1,0);
	
	glClear(GL_COLOR_BUFFER_BIT); // clear the screen
        glColor3d(0,0,0); // draw black lines
        axis(0.5);					 // z-axis
	glPushMatrix(); 
	glRotated(90, 0,1.0, 0);
	axis(0.5);					// y-axis
	glRotated(-90.0, 1, 0, 0);
	axis(0.5);					// z-axis
	glPopMatrix();	

	glPushMatrix();
    glScaled(2.0, 2.0, 2.0); 
	glTranslated(1,1,1);
	
	glutWireTeapot(0.2); // teapot at (1,1,1)
	glPopMatrix();

	glFlush();
}

void tecrado(unsigned char tecra, int mouseX, int mouseY)
{
	switch(tecra)
	{
		case'v':
			posCam.set(posCam.getX()+0.2,posCam.getY()+0.2,posCam.getZ()+0.2);
		break;
		case'V':
			posCam.set(posCam.getX()-0.2,posCam.getY()-0.2,posCam.getZ()-0.2);
		break;
		case'x':
			centro.setX(centro.getX()+0.2);
		break;		
		case'X':
			centro.setX(centro.getX()-0.2);
		break;
		case'y':
			centro.setY(centro.getY()+0.2);
		break;
		case'Y':
			centro.setY(centro.getY()-0.2);
		break;
		case'z':
			centro.setZ(centro.getZ()+0.2);
		break;
		case'Z':
			centro.setZ(centro.getZ()-0.2);
		break;
		case'a':
			posCam.setX(posCam.getX()+0.2);
		break;
		case'A':
			posCam.setX(posCam.getX()-0.2);
		break;
		case'w':
			posCam.setY(posCam.getY()+0.2);
		break;
		case'W':
			posCam.setY(posCam.getY()-0.2);
		break;
		case's':
			posCam.setZ(posCam.getZ()+0.2);
		break;
		case'S':
			posCam.setZ(posCam.getZ()-0.2);
		break;
	}
	displayWire();
}


//<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
	glutInitWindowSize(640,480);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Transformation testbed - wireframes");
	glutKeyboardFunc(tecrado);
	glutDisplayFunc(displayWire);
	glClearColor(1.0f, 1.0f, 1.0f,0.0f);  // background is white
	glViewport(0, 0, 640, 480);

	glutMainLoop();
}
